package com.ebelter.myehc.ui.fragments.trends;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.CommonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.views.ChartView3;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.btcomlib.views.CombinView8;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.response.device_resports.BRA_DuanLian_R;
import com.ebelter.myehc.utils.EHCCommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8SrcRes;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8Title;


/**
 * 趋势血压
 */

public class Trend_Bra_duanlian_Fragment extends BaseFragment {
    public static final String TAG = "Trend_TiWen_Fragment";
    private static final int[] levelColors = {0xff14e34e, 0xffffcc00};


    CombinView7 bra_dl_bushu_cb7;
    CombinView8 bra_dl_bushu_cb8;
    CombinView8 bra_dl_distance_cb8;
    CombinView8 bra_dl_carili_cb8;

    ChartView3 dl_steps_chartview3;
    ChartView3 dl_distance_chartview3;
    ChartView3 dl_carili_chartview3;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_bra_duanlian;
    }


    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        bra_dl_bushu_cb7 = mRootView.findViewById(R.id.bra_dl_bushu_cb7);
        bra_dl_bushu_cb7.setDisplay31(true);

        bra_dl_bushu_cb8 = mRootView.findViewById(R.id.bra_dl_bushu_cb8);
        bra_dl_distance_cb8 = mRootView.findViewById(R.id.bra_dl_distance_cb8);
        bra_dl_carili_cb8 = mRootView.findViewById(R.id.bra_dl_carili_cb8);

        bra_dl_bushu_cb8.setTitle5Display(true);
        bra_dl_distance_cb8.setTitle5Display(true);
        bra_dl_carili_cb8.setTitle5Display(true);

        dl_steps_chartview3 = mRootView.findViewById(R.id.dl_steps_chartview3);
        dl_distance_chartview3 = mRootView.findViewById(R.id.dl_distance_chartview3);
        dl_carili_chartview3 = mRootView.findViewById(R.id.dl_carili_chartview3);
        if (AppUtils.en.equals(AppUtils.getLocalStr())) {
            bra_dl_bushu_cb7.setTitleSizes(13);
            bra_dl_bushu_cb8.setTitleSizes(14);
            bra_dl_distance_cb8.setTitleSizes(14);
            bra_dl_carili_cb8.setTitleSizes(14);
        }

        dl_steps_chartview3.warringTem = -100000;
        dl_steps_chartview3.yOriStart = 0;
        dl_steps_chartview3.displayInt = true;

        dl_distance_chartview3.warringTem = -1000;
        dl_distance_chartview3.yOriStart = 0;

        dl_carili_chartview3.warringTem = -1000;
        dl_carili_chartview3.yOriStart = 0;
        dl_carili_chartview3.displayInt = true;
    }


    public void setDisData(BRA_DuanLian_R baseResponse) {
        if (baseResponse == null || baseResponse.resultData == null) return;


        List<String> stepsXValue = new ArrayList<>();
        List<String> distanceXValue = new ArrayList<>();
        List<String> caruliXValue = new ArrayList<>();

        List<String> stepsDisXValue = new ArrayList<>();
        List<String> distanceDisXValue = new ArrayList<>();
        List<String> caruliDisXValue = new ArrayList<>();

        Map<String, Float> stepsValue = new HashMap<>();
        Map<String, Float> distanceValue = new HashMap<>();
        Map<String, Float> caruliValue = new HashMap<>();

        List<Integer> xPointColor = new ArrayList<>();

        float newestSteps = 0;
        float newestDistance = 0;
        float newestCaruli = 0;

        int maxSteps = 10;
        float maxDistances = 1;
        float maxCarili = 10;

        if (baseResponse.resultData.chartList != null) {

            for (int i = 0; i < baseResponse.resultData.chartList.size(); i++) {
                BRA_DuanLian_R.ResultDataBean.ChartListBean data = baseResponse.resultData.chartList.get(i);

                String timeParam = "";
                String disTimeParam = "";
                if (baseResponse.resultData.dateType == 1) {//年
                    Date date = TimeUtils.parseFormatter91Time(data.timeParam);
                    timeParam = CommonUtils.getMonths(NumUtils.string2Int(TimeUtils.formatTime12(date.getTime())));
                    disTimeParam = timeParam;
                } else if (baseResponse.resultData.dateType == 2) {//月
                    timeParam = String.format(getString_(R.string.Week_int), data.timeParam);
                    disTimeParam = timeParam;
                } else if (baseResponse.resultData.dateType == 3) {//z周
                    Date date2 = TimeUtils.parseFormatter3Time(data.timeParam);
                    timeParam = TimeUtils.formatTime11(date2.getTime());//M-d
                    disTimeParam = timeParam;
                } else if (baseResponse.resultData.dateType == 4) {//天
                    Date date3 = TimeUtils.parseFormatter1Time(data.timeParam);
                    timeParam = TimeUtils.formatTime13(date3.getTime());
                    disTimeParam = TimeUtils.formatTime4(date3.getTime());//HH:mm

                }


                if (baseResponse.resultData.dateType == 4) {//天 显示每个小时的平均值

                    if (data.hourStepsAvg > 0) {
                        if (data.hourStepsAvg > maxSteps) {
                            maxSteps = (int) data.hourStepsAvg;
                        }
                        stepsXValue.add(timeParam);
                        stepsDisXValue.add(disTimeParam);
                        stepsValue.put(timeParam, data.hourStepsAvg);
                        newestSteps = data.hourStepsAvg;
                    }

                    float disAvg = NumUtils.numBaoLiuWei2(data.hourDistancesAvg);
                    if (disAvg > 0) {
                        if (disAvg > maxDistances) {
                            maxDistances = disAvg;
                        }
                        distanceXValue.add(timeParam);
                        distanceDisXValue.add(disTimeParam);
                        distanceValue.put(timeParam, disAvg);
                        newestDistance = data.hourDistancesAvg;
                    }

                    int hc = NumUtils.numBaoLiuWei0(data.hourCaloriesAvg);
                    if (hc > 0) {
                        if (hc > maxCarili) {
                            maxCarili = hc;
                        }
                        caruliXValue.add(timeParam);
                        caruliDisXValue.add(disTimeParam);
                        caruliValue.put(timeParam, (float) hc);
                        newestCaruli = data.hourCaloriesAvg;
                    }


                } else {


                    if (data.hourStepsSum > 0) {
                        if (data.hourStepsSum > maxSteps) {
                            maxSteps = (int) data.hourStepsSum;
                        }
                        stepsXValue.add(timeParam);
                        stepsDisXValue.add(disTimeParam);
                        stepsValue.put(timeParam, data.hourStepsSum);
                        newestSteps = data.hourStepsSum;
                    }

                    float disAvg = NumUtils.numBaoLiuWei2(data.hourDistancesSum);
                    if (disAvg > 0) {
                        if (disAvg > maxDistances) {
                            maxDistances = disAvg;
                        }
                        distanceXValue.add(timeParam);
                        distanceDisXValue.add(disTimeParam);
                        distanceValue.put(timeParam, disAvg);
                        newestDistance = data.hourDistancesSum;
                    }

                    int hc = NumUtils.numBaoLiuWei0(data.hourCaloriesSum);
                    if (hc > 0) {
                        if (hc > maxCarili) {
                            maxCarili = hc;
                        }
                        caruliXValue.add(timeParam);
                        caruliDisXValue.add(disTimeParam);
                        caruliValue.put(timeParam, (float) hc);
                        newestCaruli = data.hourCaloriesSum;
                    }


                }


                xPointColor.add(levelColors[0]);
            }


            //y轴坐标对应的数据
            List<Float> stepsYValue = new ArrayList<>();
            List<Float> distanceYValue = new ArrayList<>();
            List<Float> caruliYValue = new ArrayList<>();
            for (int i = 0; i < 7; i++) {//Y轴的刻度值
                stepsYValue.add((float) (i * maxSteps * 1.0 / 5));
                distanceYValue.add((float) (i * maxDistances * 1.0 / 5));
                caruliYValue.add((float) (i * maxCarili * 1.0 / 5));
            }


            if (dl_steps_chartview3 != null) {
                dl_steps_chartview3.setxPointColor(xPointColor);
                dl_steps_chartview3.setdisXValue(stepsDisXValue);
                dl_steps_chartview3.setValue(stepsValue, stepsXValue, stepsYValue);
                dl_steps_chartview3.src2end();
            }
            if (dl_distance_chartview3 != null) {
                dl_distance_chartview3.setxPointColor(xPointColor);
                dl_distance_chartview3.setdisXValue(distanceDisXValue);
                dl_distance_chartview3.setValue(distanceValue, distanceXValue, distanceYValue);
                dl_distance_chartview3.src2end();
            }
            if (dl_carili_chartview3 != null) {
                dl_carili_chartview3.setxPointColor(xPointColor);
                dl_carili_chartview3.setdisXValue(caruliDisXValue);
                dl_carili_chartview3.setValue(caruliValue, caruliXValue, caruliYValue);
                dl_carili_chartview3.src2end();
            }


        }


        if (baseResponse.resultData.summaryList != null && baseResponse.resultData.summaryList.size() > 0) {


            setCb8Title(bra_dl_bushu_cb8, 2, NumUtils.disIntZero(baseResponse.resultData.summaryList.get(0).hourStepsMin, N2));
            setCb8Title(bra_dl_bushu_cb8, 3, NumUtils.disIntZero(baseResponse.resultData.summaryList.get(0).hourStepsMax, N2));
            setCb8Title(bra_dl_bushu_cb8, 4, NumUtils.disIntZero(baseResponse.resultData.summaryList.get(0).hourStepsAvg, N2));
            setCb8Title(bra_dl_bushu_cb8, 5, NumUtils.disIntZero(baseResponse.resultData.summaryList.get(0).hourStepsSum, N2));
            setCb8SrcRes(bra_dl_bushu_cb8, EHCCommonUtils.getDisplaySrcRes(baseResponse.resultData.summaryList.get(0).hourStepsAvg, newestSteps));


            setCb8Title(bra_dl_distance_cb8, 2, NumUtils.floatDis2XSH_2(baseResponse.resultData.summaryList.get(0).hourDistancesMin, N2));
            setCb8Title(bra_dl_distance_cb8, 3, NumUtils.floatDis2XSH_2(baseResponse.resultData.summaryList.get(0).hourDistancesMax, N2));
            setCb8Title(bra_dl_distance_cb8, 4, NumUtils.floatDis2XSH_2(baseResponse.resultData.summaryList.get(0).hourDistancesAvg, N2));
            setCb8Title(bra_dl_distance_cb8, 5, NumUtils.floatDis2XSH_2(baseResponse.resultData.summaryList.get(0).hourDistancesSum, N2));
            setCb8SrcRes(bra_dl_distance_cb8, EHCCommonUtils.getDisplaySrcRes(baseResponse.resultData.summaryList.get(0).hourDistancesAvg, newestDistance));


            setCb8Title(bra_dl_carili_cb8, 2, NumUtils.disIntZero(NumUtils.numBaoLiuWei0(baseResponse.resultData.summaryList.get(0).hourCaloriesMin), N2));
            setCb8Title(bra_dl_carili_cb8, 3, NumUtils.disIntZero(NumUtils.numBaoLiuWei0(baseResponse.resultData.summaryList.get(0).hourCaloriesMax), N2));
            setCb8Title(bra_dl_carili_cb8, 4, NumUtils.disIntZero(NumUtils.numBaoLiuWei0(baseResponse.resultData.summaryList.get(0).hourCaloriesAvg), N2));
            setCb8Title(bra_dl_carili_cb8, 5, NumUtils.disIntZero(NumUtils.numBaoLiuWei0(baseResponse.resultData.summaryList.get(0).hourCaloriesSum), N2));
            setCb8SrcRes(bra_dl_carili_cb8, EHCCommonUtils.getDisplaySrcRes(baseResponse.resultData.summaryList.get(0).hourCaloriesAvg, newestCaruli));


        }


    }

}
